﻿using FarmSmartSleep.Common.Domain;
using FarmSmartSleep.Common.Domain.BeginTable;
using FarmSmartSleep.Common.Domain.RBACFile;
using FarmSmartSleep.Common.Dtos;
using FarmSmartSleep.Common.Infrastructure;
using MediatR;

namespace FarmSmartSleep.Organ.API.Application.SleepInfoCommands.FemaleBeginCommands
{
    public class ProBeginSearchHandler : IRequestHandler<ProBeginSearchCommand, ResultPage<ProbeginDto>>
    {
        private readonly IBaseRepository<ProBegin> probeginbaseRepository;
        private readonly IBaseRepository<SleepInfor> sleepbaseRepository;
        private readonly IBaseRepository<Cottages> cottagebaseRepository;
        private readonly IBaseRepository<Fields> fieldbaseRepository;
        private readonly IBaseRepository<Employee> employeeRepository;

        public ProBeginSearchHandler(IBaseRepository<ProBegin> probeginbaseRepository, IBaseRepository<Fields> fieldbaseRepository, IBaseRepository<Cottages> cottagebaseRepository, IBaseRepository<SleepInfor> sleepbaseRepository, IBaseRepository<Employee> employeeRepository)
        {
            this.probeginbaseRepository = probeginbaseRepository;
            this.fieldbaseRepository = fieldbaseRepository;
            this.cottagebaseRepository = cottagebaseRepository;
            this.sleepbaseRepository = sleepbaseRepository;
            this.employeeRepository = employeeRepository;
        }

        /// <summary>
        /// 后裔期初查询列表
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<ResultPage<ProbeginDto>> Handle(ProBeginSearchCommand request, CancellationToken cancellationToken)
        {

            int n = 1;
            var result = new ResultPage<ProbeginDto>() { Code = (int)CodeEnum.search };
            var list = (from a in probeginbaseRepository.GetAll().Where(c => !c.Deletes)
                        join b in cottagebaseRepository.GetAll() on a.CottagesId equals b.CottagesId
                        join c in fieldbaseRepository.GetAll() on a.FieId equals c.FieIdId
                        join d in sleepbaseRepository.GetAll() on a.SleepInforId equals d.SleepInforId
                        join f in employeeRepository.GetAll() on a.CreateId equals f.EmployeeId
                        select new ProbeginDto()
                        {
                            ProBeginId = a.ProBeginId,
                            SleepInforId = a.SleepInforId,
                            CottagesId = b.CottagesId,
                            BirsdayName = c.FieIdId,
                            Stage = a.Stage,
                            ProSum = a.ProSum,
                            CottageName = b.CottageName,
                            FieIdName = c.FieIdName,
                            EarNumber = d.EarNumber,
                            ProCode = a.ProCode,
                            ProData = a.ProData,
                            ProBeginWeight = a.ProBeginWeight,
                            CreateTime = a.CreateTime,
                            UpdateTime = a.UpdateTime,
                            ExamTime = a.ExamTime,
                            CreateName = f.FirstName,
                            ExamId = a.ExamId,
                            CreateId = a.CreateId,
                            UpdateId = a.UpdateId
                        });

            foreach (var item in list)
            {
                if (item.UpdateId > 0)
                {
                    item.UpdateName = employeeRepository.GetAll().FirstOrDefault(c => c.EmployeeId == item.UpdateId).FirstName;
                }
                if (item.ExamId > 0)
                {
                    item.ExamName = employeeRepository.GetAll().FirstOrDefault(c => c.EmployeeId == item.ExamId).FirstName;
                }
            }
            if (!string.IsNullOrEmpty(request.SleepInforIds))
            {
                list = list.Where(c => c.EarNumber.Contains(request.SleepInforIds));
            }




            result.totalCount = list.ToList().Count;
            result.Data = list.ToList().OrderBy(c=>c.ProBeginId).Skip((request.PageIndex-1)*request.PageSize).Take(request.PageSize).ToList();
            result.totalPage = (int)Math.Ceiling(result.totalCount * 1.0 / request.PageSize); 
            return result;
        }
    }
}
